// $Id$


/**
 * Reversi Main Window definition file.
 *
 * @file ReversiMainWindow.cpp
 * @author Wim Leers
 */


#include "ReversiMainWindow.h"
#include <QGridLayout>
#include <QLabel>
#include <QMessageBox>
#include <QMenuBar>
#include "NewGameDialog.h"


//----------------------------------------------------------------------------
// Constructors & destructor.

ReversiMainWindow::ReversiMainWindow(QWidget * parent) : QMainWindow(parent) {
	GenerateGrid();
	SetupUI();
}


//----------------------------------------------------------------------------
// Qt: public slots.

/**
 * Toon de highscores.
 */
void ReversiMainWindow::ShowHighscores(void) {
	QMessageBox::information(this, "Highscores", "blabla");
}

/**
 * Creëer een New Game Dialog.
 */
void ReversiMainWindow::SpawnNewGameDialog(void) {
	NewGameDialog * d = new NewGameDialog();
	d->show();	
}


//----------------------------------------------------------------------------
// Private methods.

void DisableGameButtons(void) {
	
}
		
/**
 * Creëer het rooster van knoppen.
 */
void ReversiMainWindow::GenerateGrid(void) {
	// Alloceer.
	bGrid = new ReversiButton * [GRID_SIZE];
	for (unsigned int x = 0; x < GRID_SIZE; x++)
		for (unsigned int y = 0; y < GRID_SIZE; y++)
			bGrid[x] = new ReversiButton[GRID_SIZE];
	
	// Override alle buttons.
	for (unsigned int x = 0; x < GRID_SIZE; x++)
		for (unsigned int y = 0; y < GRID_SIZE; y++) {
			bGrid[x][y].SetX(x);
			bGrid[x][y].SetY(y);
		}
}

/**
 * Maak de UI aan.
 */
void ReversiMainWindow::SetupUI(void) {
	// "Onzichtbare" widgets.
	QWidget * central = new QWidget(this);
	QGridLayout * grid = new QGridLayout(central);
	QWidget * bottomRow = new QWidget(this);
	QHBoxLayout * hLayout = new QHBoxLayout(bottomRow);

	// Zichtbare widgets.
	m_blackScore = new QLineEdit(bottomRow);
	m_whiteScore = new QLineEdit(bottomRow);
	bUndo = new QToolButton(bottomRow);
	bRedo = new QToolButton(bottomRow);
	
	// Menu's.
	QMenu * mMenu = menuBar()->addMenu("Menu");
	QAction * aNew = mMenu->addAction("New Game");
	QAction * aHighscores = mMenu->addAction("Highscores");
	QAction * aQuit = mMenu->addAction("Quit");
	
	// Plaats de widgets in de gewenste lay-out.
	for (unsigned int x = 0; x < GRID_SIZE; x++)
		for (unsigned int y = 0; y < GRID_SIZE; y++)
			grid->addWidget(&bGrid[x][y], x, y);
	grid->addWidget(bottomRow, GRID_SIZE, 0, 1, GRID_SIZE);
	hLayout->addWidget(new QLabel("Zwart"));
	hLayout->addWidget(m_blackScore);
	hLayout->addWidget(new QLabel("Wit"));
	hLayout->addWidget(m_whiteScore);
	hLayout->addWidget(bUndo);
	hLayout->addWidget(bRedo);
	setCentralWidget(central);
	
	// Venster titel.
	setWindowTitle("Reversi");

	// Zorg dat de huidige standen niet aanpasbaar zijn.
	m_blackScore->setFocusPolicy(Qt::NoFocus);
	m_blackScore->setReadOnly(true);
	m_whiteScore->setFocusPolicy(Qt::NoFocus);
	m_whiteScore->setReadOnly(true);

	// Instellingen voor undo/redo knoppen.
	bUndo->setIconSize(QSize(40, 40));
	bUndo->setIcon(QIcon("icons/undo.png"));
	bRedo->setIconSize(QSize(40, 40));
	bRedo->setIcon(QIcon("icons/redo.png"));
	
	// Shortcuts.
	bUndo->setShortcut(tr("Ctrl+Z"));
	bRedo->setShortcut(tr("Ctrl+Y"));
	aNew->setShortcut(tr("Ctrl+N"));
	aHighscores->setShortcut(tr("Ctrl+H"));
	aQuit->setShortcut(tr("Ctrl+Q"));
	
	// Verbindingen.
	connect(aQuit, SIGNAL(triggered()), SLOT(close()));
	connect(aHighscores, SIGNAL(triggered()), SLOT(ShowHighscores()));
	connect(aNew, SIGNAL(triggered()), SLOT(SpawnNewGameDialog()));
}
